package com.lancoo.znbkxx.teacher.ui.fragment

import android.annotation.SuppressLint
import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.*
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.databinding.DialogEditdateBinding
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.lancoo.znbkxx.teacher.viewmodel.PaperViewModel
import kotlinx.android.synthetic.main.item_comment_questiontype.*
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit

/**
 * @author : wealive
 * @date   : 2019/12/12 14:21
 * @desc   :
 */
class DateEditDialog : AppCompatDialogFragment() ,View.OnTouchListener{
    var clickListener: View.OnClickListener? = null
//    var touchListener: View.OnTouchListener? = null
    lateinit var binding: DialogEditdateBinding
    var startTime = ""
    var endTime = ""
    var answerTime = ""
    private lateinit var scheduledExecutor: ScheduledExecutorService
    val handler = @SuppressLint("HandlerLeak")
    object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                R.id.btn_left_disable -> {
                   binding.btnRightDisable.alpha = 1.0f
                    val df = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
                    val calendar = Calendar.getInstance()
                     startTime = binding.model!!.startTime.value!!
//                    val endTime = editDialog.binding.model!!.endTime.value!!
                    calendar.time = df.parse(startTime)!!
                    calendar.add(Calendar.DAY_OF_MONTH, -1)
                    binding.model!!.startTime.value = df.format(calendar.time)
                }
                R.id.btn_right_disable -> {
                    binding.btnLeftDisable.alpha = 1.0f
                    val df = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
                    val calendar = Calendar.getInstance()
                     startTime = binding.model!!.startTime.value!!
                     endTime = binding.model!!.endTime.value!!
                    calendar.time = df.parse(endTime)!!
                    if (df.parse(startTime) == calendar.time) {
                        binding.btnRightDisable.alpha = 0.5f
                        return
                    }
                    calendar.time = df.parse(startTime)!!
                    calendar.add(Calendar.DAY_OF_MONTH, 1)
                    binding.model!!.startTime.value = df.format(calendar.time)
                }
                R.id.btn_left -> {
                    binding.btnRight.alpha = 1.0f
                    val df = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
                    val calendar = Calendar.getInstance()
                     startTime = binding.model!!.startTime.value!!
                     endTime = binding.model!!.endTime.value!!
                    calendar.time = df.parse(startTime)!!
                    if (df.parse(endTime) == calendar.time) {
                        binding.btnLeft.alpha = 0.5f
                        return
                    }
                    calendar.time = df.parse(endTime)!!
                    calendar.add(Calendar.DAY_OF_MONTH, -1)
                    binding.model!!.endTime.value = df.format(calendar.time)
                    binding.btnLeft.alpha = 1.0f
                }
                R.id.btn_right -> {
                    binding.btnLeft.alpha = 1.0f
                    if (binding.btnRightDisable.isClickable) {
                        binding.btnRightDisable.alpha = 1.0f
                    }
                    val df = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
                    val calendar = Calendar.getInstance()
                     startTime = binding.model!!.startTime.value!!
                     endTime = binding.model!!.endTime.value!!
                    calendar.time = df.parse(startTime)!!
                    calendar.add(Calendar.YEAR, 1)
                    if (df.parse(endTime) == calendar.time) {
                        binding.btnRight.alpha = 0.5f
                        return
                    }
                    calendar.time = df.parse(endTime)!!
                    calendar.add(Calendar.DAY_OF_MONTH, 1)
                    binding.model!!.endTime.value = df.format(calendar.time)
                }
            }
        }
    }
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        binding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.dialog_editdate,
            null,
            false
        )
        binding.btnLeft.setOnTouchListener(this)
        binding.btnRight.setOnTouchListener(this)
        val df = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        val calendar = Calendar.getInstance()
        calendar.time = df.parse(startTime)!!
        calendar.add(Calendar.DAY_OF_MONTH, 1)
        if (df.parse(endTime) == calendar.time) {
            binding.btnLeft.alpha = 0.5f
        } else {
            binding.btnLeft.alpha = 1.0f
        }
        calendar.time = df.parse(df.format(Date()))!!
        if (df.parse(startTime)!!.after(calendar.time)) {
            binding.tvStarttime.setTextColor(Color.parseColor("#333333"))
            binding.btnLeftDisable.alpha = 1.0f
            binding.btnRightDisable.alpha = 1.0f
            binding.btnLeftDisable.isClickable=true
            binding.btnRightDisable.isClickable=true
            binding.btnLeftDisable.setOnTouchListener(this)
            binding.btnRightDisable.setOnTouchListener(this)
        } else {
            binding.tvStarttime.setTextColor(Color.parseColor("#CCCCCC"))
            binding.btnLeftDisable.alpha = 0.5f
            binding.btnRightDisable.alpha = 0.5f
            binding.btnLeftDisable.isClickable=false
            binding.btnRightDisable.isClickable=false
        }
        binding.tvConfirm.setOnClickListener(clickListener)
        binding.model = ViewModelProvider(this).get(PaperViewModel::class.java)
        binding.lifecycleOwner = this
        binding.model!!.answerTime.value = answerTime
        binding.model!!.startTime.value = startTime
        binding.model!!.endTime.value = endTime
        return AlertDialog.Builder(requireContext()).setView(binding.root).create()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return binding.root
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouch(view: View?, event: MotionEvent?): Boolean {
        if (event?.action == MotionEvent.ACTION_DOWN) {
            updateAddOrSubtract(view?.id)
        } else if (event?.action == MotionEvent.ACTION_UP) {
            stopAddOrSubtract()
        }
        return true
    }

    private fun updateAddOrSubtract(viewId: Int?) {
        scheduledExecutor = Executors.newSingleThreadScheduledExecutor()
        scheduledExecutor.scheduleWithFixedDelay({
            val msg = Message()
            msg.what = viewId!!
            handler.sendMessage(msg)
        }, 0, 100, TimeUnit.MILLISECONDS)
    }

    private fun stopAddOrSubtract() {
        scheduledExecutor.shutdown()
    }

    override fun onResume() {
        super.onResume()
        val param = this.dialog?.window?.attributes
        param?.width = DensityUtils.dp2px(context, 300f)
        param?.height = DensityUtils.dp2px(context, 230f)
        dialog?.window?.attributes = param
    }
}